package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alipay.android.phone.mrpc.core.ad;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.inageview.ItemEntity;
import com.itboye.inageview.ListItemAdapter;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.ShoPingAdapter;
import com.itboye.mengwu.app.adapter.ShopShngPingAdapter;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.MyPingJiaBean;
import com.itboye.mengwu.data.model.MyTieZiCenterBean;
import com.itboye.mengwu.data.model.ShopPingJiabean;
import com.itboye.mengwu.data.presenter.MyTieZiCenterPresenter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class ShangPingFragment extends BaseFragment implements Observer,
		IXListViewListener {

	/** Item数据实体集合 */
	private ShopPingJiabean arrlist,arrlistTwo;
	/** ListView对象 */
	private XListView lsitviewme;
	// private RoundImage image;
	private ImageView back;
	MyTieZiCenterPresenter Presenter;
	ShopPingJiabean jiabean;
	private int page_no = 1;
	private int page_size=10;
//	LuntanTieZiBean model;
	String uid;
	int num;
	ShoPingAdapter adapter;
	
	boolean ShangLaflag =false;
//
	
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_shangping;
	}

	@Override
	public void onMyActivityCreated() {
		
		lsitviewme.setXListViewListener(this);
		lsitviewme.setPullLoadEnable(true);
		lsitviewme.setPullRefreshEnable(true);
		arrlist=new ShopPingJiabean();
		Presenter=new MyTieZiCenterPresenter(this);
		uid=(String) SPUtils.get(getActivity(), null,
				SpPublicParams.UID, "");
//		Presenter.MyPingJia(uid, max_img, page_no, page_size);
		Presenter.MyPingJia(uid,"",page_no+"",page_size+"");
		showProgressDialog("数据加载中", true);

		
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub

		ResultEntity entity = handlerError(arg1);
		if (entity != null) {
			if(entity.getEventType()==Presenter.MyPingjiaSucess){
			try {
				arrlist= (ShopPingJiabean) entity.getData();
				
				
				if (ShangLaflag == true) {
					arrlistTwo.getList().addAll(arrlist.getList());
					
					adapter.upDate(getActivity(), arrlistTwo);

					adapter.notifyDataSetChanged();
				}else{
					arrlistTwo=arrlist;
					adapter=new ShoPingAdapter(getActivity(), arrlist);
					lsitviewme.setAdapter(adapter);
					
				}
			
			
			} catch (Exception e) {
				
				ByAlert.alert("当前没有数据可加载了...");
				
			}
			}
			stopLoad();
			try {
				closeProgressDialog();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		ShangLaflag=false;
		page_no = 1;
//		page_size=10;
//		Presenter.MyPingJia(uid, max_img, page_no, page_size);
		Presenter.MyPingJia(uid,"",page_no+"",page_size+"");
		showProgressDialog("数据加载中", true);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		ShangLaflag =true;
		page_no++;
//		page_size++;
//		Presenter.MyPingJia(uid, max_img, page_no, page_size);
		Presenter.MyPingJia(uid,"",page_no+"",page_size+"");
		showProgressDialog("数据加载中", true);
	}

	public void stopLoad() {
		
		lsitviewme.stopLoadMore();
		lsitviewme.stopRefresh();
	}

}
